﻿Imports System.Text.RegularExpressions
Imports DAO
Imports BUS
Imports DTO
Imports System.Linq

Public Class TiepNhanHS

    Private Sub btn_close_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_close.Click
        Close()
    End Sub

    ' Hàm kiểm tra email
    Public Shared Function IsEmail(ByVal Email As String) As Boolean
        Dim strEmail As String = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" & "\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" & ".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$"

        Dim re As New Regex(strEmail)
        If re.IsMatch(Email) Then
            Return (True)
        Else
            Return (False)
        End If
    End Function

    'Sự kiện rời khỏi txt_email sẽ ktra email có hợp lệ hay không
    Private Sub txt_email_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txt_email.Leave
        If (IsEmail(txt_email.Text.Trim()) = False) Then
            lbl_email.Text = "Email khong hop le"
            txt_email.Focus()
            Return
        Else
            lbl_email.Text = "Email hop le"
        End If
    End Sub

    'Reset các txt về rỗng
    Private Sub btn_reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_reset.Click
        txt_name.Text = ""
        lbl_name.Text = "(*)"
        lbl_birthday.Text = "(*)"
        txt_address.Text = ""
        lbl_address.Text = "(*)"
        lbl_email.Text = "(*)"
        txt_email.Text = ""
        lbl_message.Text = ""
    End Sub

    Public Function IsBorn(ByVal born As Date) As Boolean
        If ((Date.Today.Year - date_birthday.Value.Year > 20) Or (Date.Today.Year - date_birthday.Value.Year < 15)) Then
            Return False
        Else
            Return True
        End If
    End Function

    'Kiểm tra học sinh phải có tuổi từ 15 tới 20 tuổi
    Private Sub dt_born_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles date_birthday.Leave
        Dim ngaysinh As Date = New Date()
        If (Me.IsBorn(date_birthday.Value) = False) Then
            lbl_birthday.Text = "Học sinh phải từ 15 tới 20 tuổi"
        Else
            lbl_birthday.Text = "Đạt yêu cầu"
        End If

    End Sub

    Private Sub btn_accept_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_accept.Click
        If (txt_name.Text.Trim() = "") Then
            lbl_name.Text = "Tên không được trống!!!"
            txt_name.Focus()
        ElseIf (Me.IsBorn(date_birthday.Value) = False) Then
            lbl_birthday.Text = "Học sinh phải từ 15 tới 20 tuổi"
            date_birthday.Focus()
        ElseIf (txt_address.Text.Trim() = "") Then
            lbl_address.Text = "Thông tin bắt buộc"
            txt_address.Focus()
        ElseIf (IsEmail(txt_email.Text.Trim()) = False) Then
            lbl_email.Text = "Thông tin bắt buộc"
            txt_email.Focus()
        Else
            Dim hsDto As HocSinhDTO = New HocSinhDTO()
            hsDto.TenHS = txt_name.Text
            hsDto.NgaySinh = date_birthday.Value
            If (rd_male.Checked) Then
                hsDto.GioiTinh = 1
            Else
                hsDto.GioiTinh = 2
            End If
            hsDto.DiaChi = txt_address.Text
            hsDto.Email = txt_email.Text
            'hsDto.MaK = Integer.Parse(cbx_khoi.SelectedValue)
            Dim khoi As KhoiDTO = cbx_block.SelectedValue
            hsDto.MaK = khoi.MaKhoi


            Dim hsBus As HocSinhBUS = New HocSinhBUS
            hsBus.addStudent_BUS(hsDto)

            lbl_message.Text = "Thêm thành công"
        End If
    End Sub

    Private Sub TiepNhanHS_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        date_birthday.Format = DateTimePickerFormat.Custom
        date_birthday.CustomFormat = "dd/MM/yyyy"

        Dim khoi As KhoiBUS = New KhoiBUS()
        cbx_block.DisplayMember = "TenKhoi"
        cbx_block.ValueMember = "MaK"

        cbx_block.DataSource = khoi.getLevel_BUS()
    End Sub

    Private Sub cbx_khoi_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbx_block.SelectedIndexChanged

    End Sub
End Class